package cn.gdut.chengcai.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gdut.chengcai.base.JsonUtil;
import cn.gdut.chengcai.base.MessageUtil;
import cn.gdut.chengcai.base.ResponseData;
import cn.gdut.chengcai.base.SystemSettingUtil;
import cn.gdut.chengcai.service.KeyValueInfoService;
import cn.gdut.chengcai.vo.SystemSettingVo;

@Controller
@RequestMapping("/admin/system")
public class Admin_SystemController {

	@Autowired
	protected KeyValueInfoService keyValueInfoService;

	/**
	 * 系统设置页面
	 */
	@RequestMapping("/setting")
	public String systemSetting(HttpSession session, ModelMap map) {

		SystemSettingVo vo = SystemSettingUtil.getVo(keyValueInfoService, session);
		map.put("entity", vo);
		return "/admin/system/setting";
	}

	/**
	 * 系统设置保存操作
	 */
	@RequestMapping(value = "/setting/saveAction", method = { RequestMethod.POST }, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public ResponseData<Object> saveSystemSettingAction(@RequestBody String jsonString, HttpServletRequest request,
			HttpSession session) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		try {
			SystemSettingVo vo = (SystemSettingVo) JsonUtil.fromJson(jsonString, SystemSettingVo.class);
			SystemSettingUtil.updateDb(keyValueInfoService, vo);
			result.setIsSuccess(true);
		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
			// log.error(e.getMessage(), e);
		}
		return result;
	}

}